﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiceGame
{
    /// <summary>
    /// 编写一个小游戏，和电脑比掷骰子，玩家和电脑各有6个塞子，比大小。
    /// 玩家和电脑各100个游戏币，赢光对方的游戏币则获胜，否则失败（选做）
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            // 双方游戏币
            int playerCoin = 100;
            int comCoin = 100;
            // 双方的骰子点数
            int[] playerDices = new int[6];
            int[] comDices = new int[6];
            // 随机工具
            Random rnd = new Random();
            Console.WriteLine("掷骰子游戏开始！");

            while(playerCoin>0&&comCoin>0)
            {
                // 投币
                Console.WriteLine($"您有{playerCoin}个游戏币，电脑有{comCoin}个游戏币");
                Console.Write("请您输入投币的数量(1-100)：");

                int betCoin;

                while(!int.TryParse(Console.ReadLine(),out betCoin)||betCoin < 1 || betCoin > 100)
                    Console.Write("请您输入投币的数量(1-100)：");

                // 随机  掷骰子
                for (int i = 0; i < playerDices.Length; i++)
                {
                    playerDices[i] = rnd.Next(1, 7);
                    comDices[i] = rnd.Next(1, 7);
                }
                //string.Join 格式化输入数组  string.Join(间隔字符，数组)
                Console.WriteLine($"您掷出的骰子分别是{string.Join(",",playerDices)}");
                Console.WriteLine($"电脑掷出的骰子分别是{string.Join(",", comDices)}");
                                
                int playerPoints = 0;
                int comPoints = 0;

                // 求双方的点数和
                for (int i = 0; i < playerDices.Length; i++)
                {
                    playerPoints += playerDices[i];
                    comPoints += comDices[i];
                }
                Console.WriteLine($"您掷出的骰子点数是{playerPoints}，电脑掷出的骰子点数是{comPoints}");
                // 判断输赢
                if(playerPoints>comPoints)
                {
                    playerCoin += betCoin;
                    comCoin -= betCoin;
                    Console.WriteLine("你赢了一局！");
                }
                else if (playerPoints < comPoints)
                {
                    playerCoin -= betCoin;
                    comCoin += betCoin;
                    Console.WriteLine("电脑赢了一局！");
                }
                else
                    Console.WriteLine("这一局是平局！");
            }

            if (playerCoin == 0)
                Console.WriteLine("你输了游戏，Game over!");
            else
                Console.WriteLine("你赢了游戏，You Win!");

            Console.ReadKey();
        }
    }
}
